#include "myelf.h"
#include "new_sprintf.h"
/*
    演示通过输入在rodata段中的起始地址，输出对应位置字符串的功能
    输入第三个参数为通过objdump命令反汇编之后找到的字符串在rodata段中的起始地址的16进制字符串
*/
unsigned int hexStringToUInt(const char *hexStr)
{
    unsigned int result;
    char *endptr;
    result = (unsigned int)strtol(hexStr, &endptr, 16);

    if (endptr == hexStr)
    {
        return 0;
    }
    else
    {
        if (*endptr != '\0')
        {
            return 0;
        }
    }

    return result;
}
int main(int argc, char **argv)
{

    if (argc != 3)
    {
        printf("Usage: ./test2 test2 address\n");
        return 0;
    }
    rodata_info_t *info_p = load_elf_file(argv[1]);
    if (NULL != info_p)
    {
        if (64 == info_p->class_type)
        {
            printf("get string from rodata : %s\n", buf2str_addr64(info_p, hexStringToUInt(argv[2]),0));
        }
    }
    else
    {
        printf("error when load_elf_file\n");
    }
    free_elf_resource(info_p);
    return 0;
}